package cn.edu.nhic.nhcarsb.controller.admin;

import cn.edu.nhic.nhcarsb.controller.BaseController;
import cn.edu.nhic.nhcarsb.entity.Admin;
import cn.edu.nhic.nhcarsb.entity.R;
import cn.edu.nhic.nhcarsb.service.AdminService;
import cn.edu.nhic.nhcarsb.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/adminLogin")
public class AdminLoginController extends BaseController {
    @Autowired
    public AdminService adminService;
    @PostMapping
    public R login(@RequestBody Admin admin){
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Admin::getAname,admin.getAname()).eq(Admin::getApwd,admin.getApwd());
        Admin loginAdmin = adminService.getOne(lambdaQueryWrapper);
        if(!ObjectUtils.isEmpty(loginAdmin)){
            String token = JwtUtils.createAdminToken(loginAdmin.getAid());
            return R.success(token);
        }else{
            return R.error("用户名或密码错误！");
        }
    }
}
